package com.github.thread.threadLocal;

/**
 * InheritableThreadLocal特性
 * 线程上下文环境从父线程传递到子线程
 */
public class InheritableThreadLocalTest1 {
    private static java.lang.InheritableThreadLocal<Integer> requestIdThreadLocal = new java.lang.InheritableThreadLocal<>();

    public static void main(String[] args) {
        Integer reqId = new Integer(5);
        InheritableThreadLocalTest1 a = new InheritableThreadLocalTest1();
        a.setRequestId(reqId);
    }

    public void setRequestId(Integer requestId) {
        requestIdThreadLocal.set(requestId);
        doBussiness();
    }

    public void doBussiness() {
        System.out.println("首先打印requestId:" + requestIdThreadLocal.get());
        (new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("子线程启动");
                System.out.println("在子线程中访问requestId:" + requestIdThreadLocal.get());
            }
        })).start();
    }

}
